package com.tcm.doctor.service;


import cn.hutool.core.util.ObjectUtil;
import com.base.core.exception.ServiceException;
import com.base.core.lang.DateUtils;
import com.base.core.lang.StringUtils;
import com.base.core.utils.JacksonUtils;
import com.base.core.utils.ListUtils;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.tcm.doctor.domain.dto.DoctorServicesDTO;
import com.tcm.doctor.domain.dto.ServiceValueDTO;
import com.tcm.doctor.domain.entity.DoctorServices;
import com.tcm.doctor.domain.enums.BusinessCodeEnum;
import com.tcm.doctor.mapper.DoctorServicesMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService(version = "0.0.1-alpha", delay = -1, timeout = 6000)
public class DoctorServicesServiceImpl implements DoctorServicesService {

    @Autowired
    private DoctorServicesMapper doctorServicesMapper;

    @DubboReference(version = "0.0.1-beta",group = "cached-uid",timeout = 6000, check = false,retries = 3)
    private UidGenerator uidGenerator;

    @Override
    public String findValue(Long doctorId, String key, String defaultValue) {
        String value = doctorServicesMapper.findByKey(doctorId, key);
        if(StringUtils.isBlank(value)){
            return defaultValue;
        }
        return value;
    }

    @Override
    public <T> T findServicesObject(Long doctorId, String key, Class<T> clazz) {
        String value = findValue(doctorId, key, null);
        if(StringUtils.isNotBlank(value)){
            return JacksonUtils.toJavaObject(value, clazz);
        }
        try {
            return clazz.newInstance();
        } catch (Exception e) {
            throw new ServiceException("获取参数失败");
        }
    }

    @Override
    public List<DoctorServicesDTO> listServices(Long doctorId) {
        List<DoctorServices> doctorServicesList = doctorServicesMapper.listServicesByKey(doctorId);
        if(ListUtils.isEmpty(doctorServicesList)){
            return null;
        }
        List<DoctorServicesDTO> doctorServicesDTOS = doctorServicesList.stream()
                .map(doctorServices -> {
                    ServiceValueDTO serviceValueDTO = JacksonUtils.toJavaObject(doctorServices.getContent(), ServiceValueDTO.class);
                    DoctorServicesDTO doctorServicesDTO = new DoctorServicesDTO();
                    doctorServicesDTO.setValues(serviceValueDTO);
                    BeanUtils.copyProperties(doctorServices, doctorServicesDTO);
                    return doctorServicesDTO;
        }).collect(Collectors.toList());
        return doctorServicesDTOS;
    }

    @Override
    public List<DoctorServicesDTO> listBacthServices(List<Long> doctorIds, String group) {
        List<DoctorServices> doctorServicesList = doctorServicesMapper.listBacthServices(doctorIds, group);
        if(ListUtils.isEmpty(doctorServicesList)){
            return null;
        }
        List<DoctorServicesDTO> doctorServicesDTOS = doctorServicesList.stream()
                .map(doctorServices -> {
                    ServiceValueDTO serviceValueDTO = JacksonUtils.toJavaObject(doctorServices.getContent(), ServiceValueDTO.class);
                    DoctorServicesDTO doctorServicesDTO = new DoctorServicesDTO();
                    doctorServicesDTO.setValues(serviceValueDTO);
                    BeanUtils.copyProperties(doctorServices, doctorServicesDTO);
                    return doctorServicesDTO;
                }).collect(Collectors.toList());
        return doctorServicesDTOS;
    }

    @Override
    public void save(DoctorServicesDTO doctorServicesDTO) throws ServiceException {
        if(ObjectUtil.isEmpty(doctorServicesDTO.getName())){
            R.throwFail(BusinessCodeEnum.SERVICE_NAME_NOT_FOUND);
        }
        if(ObjectUtil.isEmpty(doctorServicesDTO.getDoctorId())){
            R.throwFail(BusinessCodeEnum.DOCTOR_ID_NOT_EXISTS);
        }
        if(ObjectUtil.isEmpty(doctorServicesDTO.getTitle())){
            R.throwFail(BusinessCodeEnum.SERVICE_TITLE_NOT_FOUND);
        }
        if(ObjectUtil.isEmpty(doctorServicesDTO.getValues())){
            R.throwFail(BusinessCodeEnum.SERVICE_VALUES_NOT_FOUND);
        }
        try {
            DoctorServices doctorServices = doctorServicesMapper.findServices(doctorServicesDTO.getDoctorId(), doctorServicesDTO.getName());
            if(ObjectUtil.isEmpty(doctorServices)){
                doctorServices = new DoctorServices();
                BeanUtils.copyProperties(doctorServicesDTO,doctorServices);
                doctorServices.setId(uidGenerator.getUID());
                doctorServices.setCreateTime(DateUtils.nowTime());
                doctorServices.setUpdateTime(DateUtils.nowTime());
                doctorServices.setGroups("consultation");
                doctorServices.setContent(JacksonUtils.toJSONString(doctorServicesDTO.getValues()));
                doctorServicesMapper.insert(doctorServices);
            }else {
                doctorServices.setUpdateTime(DateUtils.nowTime());
                doctorServices.setContent(JacksonUtils.toJSONString(doctorServicesDTO.getValues()));
                doctorServicesMapper.update(doctorServices);
            }
        }catch (ServiceException e){
            R.throwOnFail(e.getResult());
        }catch (Exception e){
            R.throwFail(e.getMessage());
        }

    }

    @Override
    public void delete(Long doctorId, String key) {
        try{
            doctorServicesMapper.findServices(doctorId, key);
        }catch (Exception e){
            R.throwFail(e.getMessage());
        }
    }
}
